import * as cc from "cc";
import { EventType } from "../GameSpecial/GameEventType";
import Loader from "./Loader";
import EventManager from "./EventManager";
import yyComponent from "./yyComponent";
import { GlobalEnum } from "../GameSpecial/GlobalEnum";

/**UI管理器 */
export default class UIManager {
    protected static node: cc.Node;
    protected static UIs;
    public static init(node: cc.Node) {
        this.node = node;
        this.UIs = {};
        for (let i = node.children.length - 1; i >= 0; --i) {
            let js = node.children[i].getComponent(yyComponent);
            if (!!js && !!js["uiType"]) {
                this.UIs[js["uiType"]] = js;
            }
        }
        this.onEvents();
    }
    protected static onEvents() {
        EventManager.on(EventType.Common.UIEvent.enter, this.enterUI, this);
        EventManager.on(EventType.Common.UIEvent.exit, this.exitUI, this);
    }

    /**获取指定UI脚本 */
    public static getUI(type) {
        if (!this.UIs[type]) {
            console.warn("UI尚未加载，无法获取：", type);
            return null;
        }
        return this.UIs[type];
    }

    /**
     * 是否存在指定的UI类型
     * 仅判断当前项目是否有对应的UI，不论该UI是否已加载
     */
    public static existUI(type): boolean {
        return !!this.node.getChildByName(type);
    }

    protected static enterUI(ui, data?: any) {
        let js = this.UIs[ui];
        if (null === js) return;
        if (!!js) {
            setTimeout(() => {
                this.showUI(ui, data);
            }, 0);
        } else {
            this.loadUI(ui, data);
        }
    }
    protected static showUI(ui, data?: any) {
        let js = this.UIs[ui];
        if (!!js) {
            js.show(data);
            EventManager.emit(EventType.Common.UIEvent.entered, ui);
        }
    }
    protected static loadUI(ui, data?: any) {
        let bundle = "UI";
        if (ui == GlobalEnum.UI.lobby) {
            bundle = "MainScene";
        }
        Loader.loadBundle(bundle, () => {
            Loader.loadBundleRes(bundle, ui + "/" + ui, (res) => {
                if (!res) {
                    this.UIs[ui] = null;
                    console.error("要显示的界面预制不存在：", ui);
                    return;
                }
                if (!!this.UIs[ui]) return;
                let node = cc.instantiate(res);
                this.node.getChildByName(ui).addChild(node);
                let wg = node.getComponent(cc.Widget);
                if (!!wg) {
                    wg.updateAlignment();
                }
                let ly = node.getComponent(cc.Layout);
                if (!!ly) {
                    ly.updateLayout();
                }
                let js = node.getComponent(yyComponent);
                js.init();
                this.UIs[ui] = js;
                this.showUI(ui, data);
            }, cc.Prefab, true);
        }, true);
    }

    protected static exitUI(ui) {
        let js = this.UIs[ui];
        if (!!js && !!js.node.active) {
            js.hide();
            EventManager.emit(EventType.Common.UIEvent.exited, ui);
        }
    }
}
